package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

//http://127.0.0.1:8201/swagger-ui.html
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    //1、查询医院表中所有记录
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
    public Result findAll(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }
    //2、删除医院设置
    @ApiOperation("逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id){
        boolean ok = hospitalSetService.removeById(id);
        if(ok){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }
    //3、条件查询带分页
    @ApiOperation("条件查询带分页设置")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result conditionSelectPage(@PathVariable long current,@PathVariable long limit,
                                      @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //前端传递：医院名称hosname，医院编号hoscode
        //条件查询，带分页。使用vo保存数据。

        //保存page分页对象
        Page<HospitalSet> page = new Page<>(current,limit);

        //QueryWrapper条件构造器
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        //添加查询条件
        if(!StringUtils.isEmpty(hosname)){
            queryWrapper.like("hosname",hosname);
        }
        if(!StringUtils.isEmpty(hoscode)){
            queryWrapper.eq("hoscode",hoscode);
        }
        //调用方法实现分页查询
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, queryWrapper);
        return  Result.ok(pageHospitalSet);
    }


    //4、添加医院设置
    @ApiOperation("添加医院设置")
    @PostMapping("saveHospotalSet")
    public Result saveHospotalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态为1，可以使用，0不可以使用
        hospitalSet.setStatus(1);
        //设置签名秘钥：随机数+MD5
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        //保存
        boolean OK = hospitalSetService.save(hospitalSet);
        if(OK){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }


    //5、根据id获取医院设置
    @ApiOperation("根据id获取医院设置")
    @GetMapping("getHospSet/{id}")
    public Result selectHospById(@PathVariable long id){
        HospitalSet hosp = hospitalSetService.getById(id);
        return Result.ok(hosp);
    }

    //6、修改医院设置
    @ApiOperation("修改医院设置")
    @PostMapping("updateHospSet")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet){
        boolean OK = hospitalSetService.updateById(hospitalSet);
        if(OK){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //7、批量删除医院设置
    @ApiOperation("批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospSet(@RequestBody List<Long> idList){
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    //8、医院设置锁定和解锁
    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable long id,@PathVariable Integer status){
        HospitalSet hosp = hospitalSetService.getById(id);
        hosp.setStatus(status);
        hospitalSetService.updateById(hosp);
        return Result.ok();
    }


    //9、发送签名秘钥
    @ApiOperation("发送签名秘钥")
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable long id){
        HospitalSet hosp = hospitalSetService.getById(id);
        String signKey = hosp.getSignKey();
        String hoscode = hosp.getHoscode();
        //todo 发送短信
        return Result.ok();
    }

}
